package com.rd.teacher.jichao.day14;

import java.util.Scanner;

public class TestABC {
    static Integer count = 1;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入循环打印的次数：");
        int cnt = scanner.nextInt();

        // 要打印的线程  和 ABC之间的关系
        // count 123456789  ABCABCABC
        // count % 3 = 1  -> A
        // count % 3 = 2  -> B
        // count % 3 = 0  -> C

        // 多个线程也可能同时打印  需要加锁
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < cnt; ) {
                    synchronized (count) {
                        if (count % 3 == 1) {
                            System.out.print("A");
                            count++;
                            i++;
                        }
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < cnt; ) {
                    synchronized (count) {
                        if (count % 3 == 2) {
                            System.out.print("B");
                            count++;
                            i++;
                        }
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < cnt; ) {
                    synchronized (count) {
                        if (count % 3 == 0) {
                            System.out.print("C");
                            count++;
                            i++;
                        }
                    }
                }
            }
        }).start();

    }
}
